import { showSuccessToast } from 'vant';
import { ref } from 'vue';

import { sendFeedBack } from '../api';
import FeedbackConfig from '../models/Feedback';

const useFeedback = () => {
  const config = ref(new FeedbackConfig());
  const pageState = ref({
    loading: false,
  });
  const form = ref({
    validate: () => Promise.resolve(),
  });

  const handleUpdate = async () => {
    try {
      pageState.value.loading = true;
      await form.value.validate();
      const { data } = await sendFeedBack(config.value);
      showSuccessToast(data.msg || '反馈成功');
    } catch (error) {
      //
    } finally {
      pageState.value.loading = false;
    }
  };
  return {
    form,
    config,
    pageState,
    handleUpdate,
  };
};

export default useFeedback;
